package com.yuncheng.spcyApi.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import org.apache.commons.collections.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuncheng.spcyApi.constant.other.PxSjConstant;
import com.yuncheng.spcyApi.constant.common.SpcyConstant;
import com.yuncheng.spcyApi.entity.*;
import com.yuncheng.exception.BusinessException;
import com.yuncheng.spcyApi.mapper.SpcyPxDjjbxxMapper;
import com.yuncheng.spcyApi.service.*;
import com.yuncheng.spcyApi.utils.GetUuIdUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.yuncheng.common.base.service.impl.BaseServiceImpl;
import utils.LogUtils;
import utils.ObjectToMapUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
 * @Description: 审评查验-培训-答卷基本信息
 * @Author: csx
 * @Date: 2023-03-03
 * @Version: V1.0
 */
@Service
public class SpcyPxDjjbxxServiceImpl extends BaseServiceImpl<SpcyPxDjjbxxMapper, SpcyPxDjjbxx> implements ISpcyPxDjjbxxService {

    @Resource
    private ISpcyPxDjdaService spcyPxDjdaService;

    @Resource
    private ISpcyPxDjtmService spcyPxDjtmService;

    @Resource
    @Lazy
    private ISpcyPxDjjbxxService spcyPxDjjbxxService;

    @Resource
    @Lazy
    private ISpcyPxSjjbxxService spcyPxSjjbxxService;

    @Resource
    @Lazy
    private ISpcyPxSjtmService spcyPxSjtmService;

    @Resource
    @Lazy
    private ISpcyPxSjtmxxService spcyPxSjtmxxService;

    @Resource
    @Lazy
    private ISpcyLogService spcyLogService;

    @Override
    public void JsDjTmScore(String djId){
        // 答卷
//        SpcyPxDjjbxx dj = spcyPxDjjbxxService.getById(djId);

        // 获取单选题的答卷题目
        List<SpcyPxDjtm> danxDjtmList = spcyPxDjtmService.list(new LambdaQueryWrapper<SpcyPxDjtm>()
                .eq(SpcyPxDjtm::getFdjid, djId)
                .eq(SpcyPxDjtm::getFtmlx, PxSjConstant.SJTM_LX_DANX));

        if (CollectionUtils.isNotEmpty(danxDjtmList)) {
            // 单选的答卷题目不为空
            for (SpcyPxDjtm djtm : danxDjtmList) {
                String djtmId = djtm.getId(); // 答卷题目id

                // 获取当前单选题目选项、为正确答案的数据
                List<SpcyPxDjda> djdaList = spcyPxDjdaService.list(new LambdaQueryWrapper<SpcyPxDjda>()
                        .eq(SpcyPxDjda::getFdjtmid, djtmId)
                        .eq(SpcyPxDjda::getFsfzqda, PxSjConstant.SJTM_DJDA_SFZQDA_YES));

                if (CollectionUtils.isNotEmpty(djdaList)) {
                    // 不为空，当前单选题目选项、为正确答案的数据
                    String yesFlag = "1"; // 全部为正确答案的选项，都选中为1(1为得分，0为不得分)
                    for (SpcyPxDjda djda : djdaList){
                        // 判断正确答案数据里面 是否选中
                        if (StringUtils.isNotEmpty(djda.getFsfxz())
                                && djda.getFsfxz().equals(PxSjConstant.SJTM_DJDA_SFXZ_NO)){
                            // 在全面为正确答案的选项中，存在某一个没有选中,即为不得分
                            yesFlag = "0";
                            break;
                        }
                    }

                    // 修改题目得分情况
                    if (yesFlag.equals("1")){
                        SpcyPxDjtm nowDjtm = spcyPxDjtmService.getById(djtmId);

                        nowDjtm.setFsfdd(PxSjConstant.SJTM_SFDD_YES); // 是否答对-是
                        nowDjtm.setFtmdf(djtm.getFtxfs());
                        spcyPxDjtmService.updateById(nowDjtm); // 更新题目得分
                    }else {
                        SpcyPxDjtm nowDjtm = spcyPxDjtmService.getById(djtmId); // 当前答卷题目

                        nowDjtm.setFsfdd(PxSjConstant.SJTM_SFDD_NO); // 是否答对-否
                        spcyPxDjtmService.updateById(nowDjtm);
                    }
                }
            }
        }


        // 获取多选题的答卷题目
        List<SpcyPxDjtm> duoxDjtmList = spcyPxDjtmService.list(new LambdaQueryWrapper<SpcyPxDjtm>()
                .eq(SpcyPxDjtm::getFdjid, djId)
                .eq(SpcyPxDjtm::getFtmlx, PxSjConstant.SJTM_LX_DUOX));
        if (CollectionUtils.isNotEmpty(duoxDjtmList)){
            // 多选的答卷题目不为空
            for (SpcyPxDjtm djtm : duoxDjtmList) {
                String djtmId = djtm.getId(); // 答卷题目id

                // 获取当前多选题目选项、为正确答案的数据
                List<SpcyPxDjda> djdaYesList = spcyPxDjdaService.list(new LambdaQueryWrapper<SpcyPxDjda>()
                        .eq(SpcyPxDjda::getFdjtmid, djtmId)
                        .eq(SpcyPxDjda::getFsfzqda, PxSjConstant.SJTM_DJDA_SFZQDA_YES));

                // 获取当前多选题目选项、为正确答案的数据
                List<SpcyPxDjda> djdaXzList = spcyPxDjdaService.list(new LambdaQueryWrapper<SpcyPxDjda>()
                        .eq(SpcyPxDjda::getFdjtmid, djtmId)
                        .eq(SpcyPxDjda::getFsfxz, PxSjConstant.SJTM_DJDA_SFXZ_YES));

                int djdaYesNumber = djdaYesList.size(); // 多选题正确答案数量

                int djdaXzNumber = djdaXzList.size(); // 选项选中数量

                // 选中数量不为空的时候
                if (CollectionUtils.isNotEmpty(djdaYesList) && CollectionUtils.isNotEmpty(djdaXzList)){

                    // 不为空，当前多选题目选项、为正确答案的数据
                    String yesFlag = "1"; // 全部为正确答案的选项，都选中为1(1为得分，0为不得分)
                    for (SpcyPxDjda djda : djdaYesList){
                        if (StringUtils.isNotEmpty(djda.getFsfxz()) && djda.getFsfxz().equals(PxSjConstant.SJTM_DJDA_SFXZ_NO)){
                            // 在全面为正确答案的选项中，存在某一个没有选中
                            yesFlag = "0";
                            break;
                        }
                    }

                    // 修改题目得分情况
                    if (yesFlag.equals("1")){
                        SpcyPxDjtm nowDjtm = spcyPxDjtmService.getById(djtmId);

                        // 答案数量 等于 选中数量
                        if (djdaYesNumber == djdaXzNumber) {
                            nowDjtm.setFsfdd(PxSjConstant.SJTM_SFDD_YES); // 是否答对-是
                            nowDjtm.setFtmdf(djtm.getFtxfs());
                        }else {
                            nowDjtm.setFsfdd(PxSjConstant.SJTM_SFDD_NO); // 是否答对-否
                        }
                        spcyPxDjtmService.updateById(nowDjtm); // 更新题目得分
                    }else {
                        // 答对的题目选项不为空
                        SpcyPxDjtm nowDjtm = spcyPxDjtmService.getById(djtmId); // 当前答卷题目

                        nowDjtm.setFsfdd(PxSjConstant.SJTM_SFDD_NO); // 是否答对-否
                        spcyPxDjtmService.updateById(nowDjtm);
                    }

                }
            }
        }

        // 2023-5-10 新增
        // 获取判断题的答卷题目
        List<SpcyPxDjtm> pdtDjtmList = spcyPxDjtmService.list(new LambdaQueryWrapper<SpcyPxDjtm>()
                .eq(SpcyPxDjtm::getFdjid, djId)
                .eq(SpcyPxDjtm::getFtmlx, PxSjConstant.SJTM_LX_PDT));

        if (CollectionUtils.isNotEmpty(pdtDjtmList)) {
            // 判断题的答卷题目不为空
            for (SpcyPxDjtm djtm : pdtDjtmList) {
                String djtmId = djtm.getId(); // 答卷题目id

                // 获取当前判断题题目选项、为正确答案的数据
                List<SpcyPxDjda> djdaList = spcyPxDjdaService.list(new LambdaQueryWrapper<SpcyPxDjda>()
                        .eq(SpcyPxDjda::getFdjtmid, djtmId)
                        .eq(SpcyPxDjda::getFsfzqda, PxSjConstant.SJTM_DJDA_SFZQDA_YES));

                if (CollectionUtils.isNotEmpty(djdaList)) {
                    // 不为空，当前单选题目选项、为正确答案的数据
                    String yesFlag = "1"; // 全部为正确答案的选项，都选中为1(1为得分，0为不得分)
                    for (SpcyPxDjda djda : djdaList){
                        // 判断正确答案数据里面 是否选中
                        if (StringUtils.isNotEmpty(djda.getFsfxz())
                                && djda.getFsfxz().equals(PxSjConstant.SJTM_DJDA_SFXZ_NO)){
                            // 在全面为正确答案的选项中，存在某一个没有选中,即为不得分
                            yesFlag = "0";
                            break;
                        }
                    }

                    // 修改题目得分情况
                    if (yesFlag.equals("1")){
                        SpcyPxDjtm nowDjtm = spcyPxDjtmService.getById(djtmId);

                        nowDjtm.setFsfdd(PxSjConstant.SJTM_SFDD_YES); // 是否答对-是
                        nowDjtm.setFtmdf(djtm.getFtxfs());
                        spcyPxDjtmService.updateById(nowDjtm); // 更新题目得分
                    }else {
                        SpcyPxDjtm nowDjtm = spcyPxDjtmService.getById(djtmId); // 当前答卷题目

                        nowDjtm.setFsfdd(PxSjConstant.SJTM_SFDD_NO); // 是否答对-否
                        spcyPxDjtmService.updateById(nowDjtm);
                    }
                }
            }
        }
    }

    @Override
    public String JsDjtmTotalScore(String djId){
        // 答卷
        SpcyPxDjjbxx dj = spcyPxDjjbxxService.getById(djId);

        String sysOrgCode = dj.getSysOrgCode();

        BigDecimal danxScore = new BigDecimal("0");
        BigDecimal duoxScore = new BigDecimal("0");
        BigDecimal zhugScore = new BigDecimal("0");
        BigDecimal pdtScore = new BigDecimal("0"); // 新增2023-5-10
        BigDecimal totalScore = new BigDecimal("0");
//        String danxScore = "0"; // 单选得分
//        String duoxScore = "0"; // 多选得分
//        String zhugScore = "0"; // 主观得分
//        String totalScore = "0"; // 总得分

        // 单选题 - 答对 的答卷题目
        List<SpcyPxDjtm> danxtYesList = spcyPxDjtmService.list(new LambdaQueryWrapper<SpcyPxDjtm>()
                .eq(SpcyPxDjtm::getFdjid, djId)
                .eq(SpcyPxDjtm::getFtmlx, PxSjConstant.SJTM_LX_DANX)
                .eq(SpcyPxDjtm::getFsfdd, PxSjConstant.SJTM_SFDD_YES));
        if (CollectionUtils.isNotEmpty(danxtYesList)){
            // 非空 单选题题目
            for (SpcyPxDjtm djtm : danxtYesList){
                // 得分不为空
                if (StringUtils.isNotEmpty(djtm.getFtmdf())){
//                    int tmdf = Integer.valueOf(djtm.getFtmdf());
                    BigDecimal tmdf = new BigDecimal(djtm.getFtmdf());
                    danxScore = danxScore.add(tmdf);
                    //BigDecimal涉及小数用这种类型
//                    danxScore += tmdf;
                }
            }
        }

        // 多选题 - 答对 的答卷题目
        List<SpcyPxDjtm> duoxtYesList = spcyPxDjtmService.list(new LambdaQueryWrapper<SpcyPxDjtm>()
                .eq(SpcyPxDjtm::getFdjid, djId)
                .eq(SpcyPxDjtm::getFtmlx, PxSjConstant.SJTM_LX_DUOX)
                .eq(SpcyPxDjtm::getFsfdd, PxSjConstant.SJTM_SFDD_YES));
        if (CollectionUtils.isNotEmpty(duoxtYesList)){
            // 非空 多选题题目
            for (SpcyPxDjtm djtm : duoxtYesList){
                // 得分不为空
                if (StringUtils.isNotEmpty(djtm.getFtmdf())){
//                    int tmdf = Integer.valueOf(djtm.getFtmdf());
//                    duoxScore += tmdf;
                    BigDecimal tmdf = new BigDecimal(djtm.getFtmdf());
                    duoxScore = duoxScore.add(tmdf);
                }
            }
        }

        // 主观题 - 答对 的答卷题目
        List<SpcyPxDjtm> zhugtYesList = spcyPxDjtmService.list(new LambdaQueryWrapper<SpcyPxDjtm>()
                .eq(SpcyPxDjtm::getFdjid, djId)
                .eq(SpcyPxDjtm::getFtmlx, PxSjConstant.SJTM_LX_ZHUG)
                .eq(SpcyPxDjtm::getFsfdd, PxSjConstant.SJTM_SFDD_YES));
        if (CollectionUtils.isNotEmpty(zhugtYesList)){
            // 非空 多选题题目
            for (SpcyPxDjtm djtm : zhugtYesList){
                // 得分不为空
                if (StringUtils.isNotEmpty(djtm.getFtmdf())){
//                    int tmdf = Integer.valueOf(djtm.getFtmdf());
//                    zhugScore += tmdf;
                    BigDecimal tmdf = new BigDecimal(djtm.getFtmdf());
                    zhugScore = zhugScore.add(tmdf);
                }
            }
        }

        // 2023-5-10 新增
        // 判断题 - 答对 的答卷题目
        List<SpcyPxDjtm> pdtYesList = spcyPxDjtmService.list(new LambdaQueryWrapper<SpcyPxDjtm>()
                .eq(SpcyPxDjtm::getFdjid, djId)
                .eq(SpcyPxDjtm::getFtmlx, PxSjConstant.SJTM_LX_PDT)
                .eq(SpcyPxDjtm::getFsfdd, PxSjConstant.SJTM_SFDD_YES));
        if (CollectionUtils.isNotEmpty(pdtYesList)){
            // 非空 判断题题目
            for (SpcyPxDjtm djtm : pdtYesList){
                // 得分不为空
                if (StringUtils.isNotEmpty(djtm.getFtmdf())){
//                    int tmdf = Integer.valueOf(djtm.getFtmdf());
                    BigDecimal tmdf = new BigDecimal(djtm.getFtmdf());
                    pdtScore = pdtScore.add(tmdf);
                    //BigDecimal涉及小数用这种类型
//                    danxScore += tmdf;
                }
            }
        }

        // 计算总得分
        totalScore = totalScore.add(danxScore);
        totalScore = totalScore.add(duoxScore);
        totalScore = totalScore.add(zhugScore);
        totalScore = totalScore.add(pdtScore); // 2023-5-10 新增

        dj.setFdf(totalScore.toString()); // 更新答卷得分
        dj.setSysOrgCode(sysOrgCode);
        spcyPxDjjbxxService.updateById(dj);

        return dj.getFdf();
    }

    @Override
    public String initDjjbxx(SpcyPxDjjbxx spcyPxDjjbxx, String ysjId) {
        // 获取原来的试卷 复制一份
        SpcyPxSjjbxx ySjjbxx = spcyPxSjjbxxService.getById(ysjId);
        if (ySjjbxx == null){
            throw new BusinessException("试卷信息不存在，请重新选择试卷，刷新重试并联系管理员！");
        }
        spcyPxDjjbxx.setId(GetUuIdUtils.ReplaceUuId());
        spcyPxDjjbxx.setFsjlx(ySjjbxx.getFsjlx());
        spcyPxDjjbxx.setFsjmc(ySjjbxx.getFsjmc());
        spcyPxDjjbxx.setFjzrq(ySjjbxx.getFjzrq());

        // 2023-6-26 新增
        spcyPxDjjbxx.setSysOrgCode(ySjjbxx.getSysOrgCode());

        spcyPxDjjbxx.setFdanxtdbt(ySjjbxx.getFdanxtdbt());
        spcyPxDjjbxx.setFduoxtdbt(ySjjbxx.getFduoxtdbt());
        spcyPxDjjbxx.setFpandtdbt(ySjjbxx.getFpandtdbt());
        spcyPxDjjbxx.setFzhugtdbt(ySjjbxx.getFzhugtdbt());

        spcyPxDjjbxxService.save(spcyPxDjjbxx); // 新增答卷
        spcyPxDjjbxx.setSysOrgCode(ySjjbxx.getSysOrgCode());
//        spcyPxDjjbxxService.updateById(spcyPxDjjbxx);

        /*String operContent = "新增信息";
        // 将map里面的属性和值转String
        Map<String, Object> beanMap = ObjectToMapUtils.objectToMap(spcyPxDjjbxx);
        operContent += LogUtils.MapToStringMethod(beanMap);
        spcyLogService.addLogs(LogConstant.LOGS_PX_DJJBXX,operContent,spcyPxDjjbxx.getId(), LogConstant.LOGS_PX_DJJBXX, SpcyConstant.LOGS_SYS_TYPE_XZ);
*/
        String djId = spcyPxDjjbxx.getId(); // 答卷id

        BigDecimal sjzf = new BigDecimal("0");

        // 获取原来的试卷题目
        List<SpcyPxSjtm> ySjtmList = spcyPxSjtmService.list(new LambdaQueryWrapper<SpcyPxSjtm>()
                .eq(SpcyPxSjtm::getFsjid, ysjId).orderByAsc(SpcyPxSjtm::getFxh));

        SpcyPxDjtm djtm = null;
        // 原试卷题目不为空
        for (SpcyPxSjtm sjtm : ySjtmList){
            djtm = new SpcyPxDjtm(); // 答卷题目
            // 复制原来试卷题目
            String djtmId = GetUuIdUtils.NotReplaceUuId(); // 答卷题目id
            djtm.setId(djtmId);
            djtm.setFdjid(djId);
            djtm.setFsjtmid(sjtm.getId()); // 原试卷题目id
            djtm.setFsjtm(sjtm.getFsjtm()); // 原试卷题目
            djtm.setFtxfs(sjtm.getFtmfs());
            djtm.setFtmlx(sjtm.getFtmlx());
            djtm.setFxh(sjtm.getFxh());
            djtm.setFsfdd(PxSjConstant.SJTM_SFDD_NO); // 是否答对-默认否
            spcyPxDjtmService.save(djtm); // 保存答卷题目

            if (StringUtils.isNotBlank(sjtm.getFtmfs())) {
                sjzf = sjzf.add(new BigDecimal(sjtm.getFtmfs()));
            }

            String ysjtmId = sjtm.getId(); // 原试卷题目id
            // 复制原试卷题目选项
            List<SpcyPxSjtmxx> ySjtmxxList = spcyPxSjtmxxService.list(new LambdaQueryWrapper<SpcyPxSjtmxx>()
                    .eq(SpcyPxSjtmxx::getFsjtmid, ysjtmId)
                    .orderByAsc(SpcyPxSjtmxx::getFxh));

            // 存在原试卷题目选项、新增答卷题目选项
            SpcyPxDjda spcyPxDjda = null;
            for (SpcyPxSjtmxx sjtmxx : ySjtmxxList){
                spcyPxDjda = new SpcyPxDjda();
                String djdaId = GetUuIdUtils.NotReplaceUuId(); // 答卷答案id
                spcyPxDjda.setId(djdaId);
                spcyPxDjda.setFxh(sjtmxx.getFxh());
                spcyPxDjda.setFdjtmid(djtmId); // 答卷题目id
                spcyPxDjda.setFsfzqda(sjtmxx.getFsfzqda()); // 是否正确答案
                spcyPxDjda.setFsjtmxx(sjtmxx.getFsjtmxx());
                spcyPxDjda.setFbz(sjtmxx.getFbz());
                spcyPxDjda.setFckda(sjtmxx.getFckda()); // 参考答案
                spcyPxDjda.setFsfxz(PxSjConstant.SJTM_DJDA_SFXZ_NO); // 是否选中-默认没有选中
                spcyPxDjdaService.save(spcyPxDjda); // 保存答卷题目选项
            }

        }

        BigDecimal dfBD = sjzf.setScale(0, RoundingMode.HALF_UP);
        spcyPxDjjbxx.setFdjzf(dfBD.toString());
        spcyPxDjjbxxService.updateById(spcyPxDjjbxx);

        return djId;
    }

    @Override
    public void deletePxdj(List<SpcyPxDjjbxx> djList, String pid, String logName, String operType) {

        if (CollectionUtil.isEmpty(djList)){
            return;
        }
        for (SpcyPxDjjbxx djjbxx : djList) {
            // 删除答卷
            spcyPxDjjbxxService.removeById(djjbxx.getId());

            String djLog = "删除答卷: ";
            djLog += LogUtils.MapToStringMethod(ObjectToMapUtils.objectToMap(djjbxx));
            spcyLogService.addLogs(logName, djLog, djjbxx.getFpxjbxxid(), operType, SpcyConstant.LOGS_SYS_TYPE_SC);

            List<SpcyPxDjtm> djtmList = spcyPxDjtmService.list(new LambdaQueryWrapper<SpcyPxDjtm>()
                    .eq(SpcyPxDjtm::getFdjid, djjbxx.getId()));
            if (CollectionUtils.isNotEmpty(djtmList)) {
                for (SpcyPxDjtm djtm : djtmList) {
                    spcyPxDjtmService.removeById(djtm.getId()); // 删除答卷题目

                    // 删除题目选项
                    spcyPxDjdaService.remove(new LambdaQueryWrapper<SpcyPxDjda>().eq(SpcyPxDjda::getFdjtmid, djtm.getId()));
                }
            }
        }

    }
}
